package com.boc.langxiang.presenter.shop;

import com.boc.langxiang.contract.shop.PostOrderContract;
import com.boc.langxiang.entity.request.PostOrderRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.PostOrderContent;
import com.boc.langxiang.net.HandleErrorSubscriber;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Li Guoxi on 2016/7/6.
 */
public class PostOrderPresenter extends SuperPresenter implements PostOrderContract, PostOrderContract.Presenter {
    private View mView;

    public PostOrderPresenter(View view) {
        super(view);
        this.mView = view;
    }

    @Override
    public void post(String ids, String addressId) {
        PostOrderRequest request = new PostOrderRequest();
        if (addressId == null) {
            mView.onError(null, "请选择收件地址");
            return;
        }
        request.setIds(ids);
        request.setAddressId(addressId);
        mView.showLoading();
        mService.postOrder(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<PostOrderContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<PostOrderContent> response) {
                        mView.hideLoading();
                        mView.onSuccess(response);
                    }
                });
    }
}
